package service;

import dao.CommonDAO;
import entity.MixPFO;
import entity.Passage;
import dao.CourseDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PassageService {

    private CommonDAO cd=new CommonDAO();
    private CourseDao courseDao = new CourseDao();

    private List<Passage> createPassageList(List<Map<String,Object>> mapList){
        List<Passage> passages=new ArrayList<>();
        for(int i = 0;i < mapList.size();i++)
            passages.add(createPassage(mapList.get(i)));
        return passages;
    }

    private Passage createPassage(Map<String,Object> tempMap){
        return new Passage( Integer.valueOf(tempMap.get("passageID").toString()),
                            tempMap.get("title").toString(),
                            tempMap.get("content").toString(),
                            tempMap.get("createDate").toString(),
                            Integer.valueOf(tempMap.get("count").toString()),
                            Integer.valueOf(tempMap.get("operator").toString()),
                            Integer.valueOf(tempMap.get("field").toString()),
                            Integer.valueOf(tempMap.get("course").toString()),
                            Integer.valueOf(tempMap.get("status").toString()),
                            Integer.valueOf(tempMap.get("roof").toString()));
    }

    //得到所有的文章信息 由status来控制是文章管理还是待审核
    public List<Passage> getPassage(int status){
        String sql="select * from passage where status=?";
        return createPassageList(cd.executeQuery(sql, new Object[]{status}));
    }

    //审核文章的搜索方法
    public List<Passage> getPassageBy(Integer field, String begin, String end){
        String sql="select * from passage where status=1 and field=? and createDate between ? and ?";
        return createPassageList(cd.executeQuery(sql, new Object[]{field,begin,end}));
    }

    //得到文章的详细内容（文章管理和审核文章的详细页都用了这个方法）
    public List<Passage> reviewDetail(Integer passageId){
        String sql="select * from passage as p,operator as o,field as f where p.operator=o.operatorId and p.field=f.fieldId and passageID=?";
        return createPassageList(cd.executeQuery(sql, new Object[]{passageId}));
    }
    public List<MixPFO> passageDetail(Integer passageId){
        List<MixPFO> mixPFOList=new ArrayList<>();
        String sql="select passageID, content,title,oname,fname ,createDate ,roof from passage as p,operator as o,field as f where p.operator=o.operatorId and p.field=f.fieldId and passageId=?";
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[]{passageId});
        for(int i=0;i<mapList.size();i++) {
            Map<String, Object> tempMap = mapList.get(i);
            MixPFO mix=new MixPFO();

            mix.setPassageID(Integer.parseInt(tempMap.get("passageID").toString()));
            mix.setCreateDate(tempMap.get("createDate").toString());
            mix.setFieldName(tempMap.get("fname").toString());
            mix.setTitle(tempMap.get("title").toString());
            mix.setOperatorName(tempMap.get("oname").toString());
            mix.setContent(tempMap.get("content").toString());
            mix.setRoof(Integer.parseInt(tempMap.get("roof").toString()));

            mixPFOList.add(mix);
        }
        return mixPFOList;
    }

    //通过栏目名称得到文章信息
    public List<Passage> getPassageByFieldName(String fieldId){
        String sql="select * from passage where field=fieldId and status=2";
        return createPassageList(cd.executeQuery(sql, new Object[0]));
    }

    public List<MixPFO> passageByFieldId(String fieldId, int page, int num_every_page){
        List<MixPFO> mixPFOList=new ArrayList<>();
        String sql="select fname,passageID,content,title,oname,createDate ,roof from passage ," +
                " field ,operator where passage.field=field.fieldId and passage.operator=operator.operatorId" +
                " and status=2 and fieldID="+fieldId + " limit "  + (page-1)*num_every_page + "," + num_every_page;
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[0]);
        for(int i=0;i<mapList.size();i++) {
            Map<String, Object> tempMap = mapList.get(i);
            MixPFO mix=new MixPFO();

            mix.setPassageID(Integer.parseInt(tempMap.get("passageID").toString()));
            mix.setCreateDate(tempMap.get("createDate").toString());
            mix.setFieldName(tempMap.get("fname").toString());
            mix.setTitle(tempMap.get("title").toString());
            mix.setOperatorName(tempMap.get("oname").toString());
            mix.setContent(tempMap.get("content").toString());
            mix.setRoof(Integer.parseInt(tempMap.get("roof").toString()));

            mixPFOList.add(mix);
        }
        return mixPFOList;
    }

    //文章管理的搜索方法
    public List<Passage> passageByTT(String title, String begin, String end){
        String sql="select * from passage where status=2 and title like \'%"+title+"%\' and createDate between ? and ?";
        return createPassageList(cd.executeQuery(sql, new Object[]{begin,end}));
    }

    public List<MixPFO> mixPFOSByTT(String title,String begin,String end){
        List<MixPFO> mixPFOList=new ArrayList<>();

        String sql="select passageID,title,oname,fname ,createDate,roof from passage as p,operator as o,field as f where p.operator=o.operatorId and p.field=f.fieldId and status=2 " +
                "and title like \'%"+title+"%\' and createDate between ? and ?";
//        System.out.println(sql);
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[]{begin,end});

        for(int i=0;i<mapList.size();i++) {
            Map<String, Object> tempMap = mapList.get(i);
            MixPFO mix=new MixPFO();

            mix.setPassageID(Integer.parseInt(tempMap.get("passageID").toString()));
            mix.setCreateDate(tempMap.get("createDate").toString());
            mix.setFieldName(tempMap.get("fname").toString());
            mix.setTitle(tempMap.get("title").toString());
            mix.setOperatorName(tempMap.get("oname").toString());
//            mix.setContent(tempMap.get("content").toString());
            mix.setRoof(Integer.parseInt(tempMap.get("roof").toString()));

            mixPFOList.add(mix);
        }
        return mixPFOList;
    }
    //待审核文章通过审核
    public void reviewInsert(Integer id){
        String sql="update passage set status=2 where passageId=?";
        cd.executeUpdate(sql, new Object[]{id});
    }

    //待审核文章未通过 文章管理的删除
    public void reviewDelete(Integer id){
        String sql="update passage set status=3 where passageId=?";
        cd.executeUpdate(sql, new Object[]{id});
    }

    //文章管理的修改
    public void passageUpdate(Integer passageId, String content,String roof){
        String sql="update passage set content=?,roof=? where passageID=?";
        cd.executeUpdate(sql,new Object[]{content,roof,passageId});
    }
    //得到修改文章和审核文章的表的内容
    public List<MixPFO> getInfo(int status, int page, int num_every_page){
        List<MixPFO> list=new ArrayList<>();
        String sql="select passageID,content,title,oName,fName ,createDate,roof from passage as p,operator as o,field as f where p.operator=o.operatorId and p.field=f.fieldId and status=?"
                + " limit "  + (page-1)*num_every_page + "," + num_every_page;
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[]{status});

        for(int i=0;i<mapList.size();i++){
            MixPFO mixPFO=new MixPFO();
            Map<String,Object> tempMap = mapList.get(i);

            mixPFO.setPassageID(Integer.parseInt(tempMap.get("passageID").toString()));
            mixPFO.setTitle(tempMap.get("title").toString());
            mixPFO.setOperatorName(tempMap.get("oName").toString());
            mixPFO.setFieldName(tempMap.get("fName").toString());
            mixPFO.setCreateDate(tempMap.get("createDate").toString());
            mixPFO.setRoof(Integer.parseInt(tempMap.get("roof").toString()));
            mixPFO.setContent(tempMap.get("content").toString());
//           List<Map<String,Object>> artical = cd.executeQuery(querysql, new Object[]{ariticle.getPassageId(),0,1});
            list.add(mixPFO);
        }
        return list;
    }

    public List<MixPFO> getMixByFT(Integer field,String begin,String end, int page, int num_every_page){
        List<MixPFO> list=new ArrayList<>();

        String sql="select passageID,title,oname,fname ,createDate,roof from passage as p,operator as o,field as f where" +
                " p.operator=o.operatorID and p.field=f.fieldID and status=1 and field=? and createDate between ? and ?" +
                " limit "  + (page-1)*num_every_page + "," + num_every_page;
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[]{field,begin,end});
        int listSize=mapList.size();
        for(int i=0;i<listSize;i++){
            MixPFO ariticle=new MixPFO();

            Map<String,Object> tempMap = mapList.get(i);
//           Map<String,Object> tempMap1 = mapList1.get(i);

            ariticle.setPassageID(Integer.parseInt(tempMap.get("passageID").toString()));
            ariticle.setTitle(tempMap.get("title").toString());
            ariticle.setOperatorName(tempMap.get("oname").toString());
            ariticle.setFieldName(tempMap.get("fname").toString());
            ariticle.setCreateDate(tempMap.get("createDate").toString());
            ariticle.setRoof(Integer.parseInt(tempMap.get("roof").toString()));
//           List<Map<String,Object>> artical = cd.executeQuery(querysql, new Object[]{ariticle.getPassageId(),0,1});
            list.add(ariticle);
        }
        return list;
    }








    //=====================================前台用=====================================

    private CourseDao dao = new CourseDao();

    public Passage getPassageByField(String field){
        return dao.getPassageByField(field);
    }

    public Passage getPassageById(String id){
        return dao.getPassageById(id);
    }

    public List<Passage> getFieldPassages(String field, int page){
        return dao.getFieldPassages(field, page);
    }

    public List<Passage> getSearchPassages(String search, int page){
        return dao.getSearchPassages(search, page);
    }

    public List<Passage> getSearchPassages(String search, String fieldId, int page){
        return dao.getSearchPassages(search, fieldId, page);
    }

    public int getNumberOfFieldPassages(String field, int status) {
        return dao.getNumberOfFieldPassages(field, status);
    }

    public int getNumberOfSearchPassages(String search){
        return dao.getNumberOfSearchPassages(search);
    }

    public int getNumberOfSearchPassages(String search, String fieldId){
        return dao.getNumberOfSearchPassages(search, fieldId);
    }

    public int getNumberOfPassage(int status){
        return dao.getNumberOfPassages(status);
    }

    public int getNumberOfPassageByFT(int field, int status, String begin, String end){
        return dao.getNumberOfPassageByFT(field, status, begin, end);
    }

    public int getNumberOfPassageByTT(String title, int status, String begin, String end){
        return dao.getNumberOfPassageByTT(title, status, begin, end);
    }
}
